<?php

namespace app\controller\web;

use app\service\AppClientTableService;
use app\service\AppInfoTableService;
use app\service\AppMchTableService;
use app\validate\AppInfoTableValidate;
use app\validate\PageValidate;
use share\service\ContextService;
use share\utils\Helper;

class AppInfoTableController extends WebBaseController{

    public function initValidate(){        
        $this->validate = new AppInfoTableValidate();
    }

    /**
     * 获取所有厂商标识
     */
    public function list_vendor_action(){  
        $data = AppInfoTableService::getListVendorInfo(null);
        return self::ok($data);
    }

    /**
     * 新增APP包
     */
    public function add_action(){
        $input = $this->check();
        $data = AppInfoTableService::addPackage($input);
        return self::ok($data);
    }

    /**
     * 修改APP包
     */
    public function edit_action(){
        $input = $this->check();
        $data = AppInfoTableService::updatePackage($input);
        return self::ok($data);
    }

    /**
     * 删除APP包
     */
    public function del_action(){
        $input = $this->check();
        $data = AppInfoTableService::delPackage($input['id']);
        return self::ok($data);
    }

    /**
     * 列表 搜索
     */
    public function list_action(){
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $input = $this->check();
        $data = AppInfoTableService::getListInfo($input,$page);
        $total = AppInfoTableService::getListCountInfo($input);     
        $data = Helper::arrayListUnderscoreToCamelKey($data);
        $data = Helper::arrayListReplaceKey($data,['isIos'=>'ios','isAndroid'=>'android']);     
        return self::ok(self::formatPageData($page,$total,$data));
    }

    /**
     * 获取包名信息
     */
    public function list_package_action(){
        $data = AppInfoTableService::getAllByFilter([]);
        $res = Helper::listArrayColumn($data,['package','app_cn' => 'appCn']);
        return self::ok($res);
    }
    
    /**
     * 获取用户可用app信息
     */
    public function get_useful_app_action(){
        $adminInfo = ContextService::getUserInfo();
        $data = [];
        if($adminInfo['level'] == 0){
            $data = AppInfoTableService::getAllByFilter([]);
        }
        else{
            $vendor = $adminInfo['vendor'];
            if($vendor){
                $data = AppInfoTableService::getAllByFilter(['vendor'=>$vendor]);
            }
        }

        $uniqueData = [];
        foreach($data as $item){
            $vendor = $item['vendor'];
            if(!isset($uniqueData[$vendor])){
                $uniqueData[$vendor] = $item;
            }
        }
        $res = Helper::listArrayColumn(array_values($uniqueData),['vendor','app_cn' => 'appName']);
        return self::ok($res);
    }

    /**
     * 获取app绑定的支付商户和三方授权关系
     */
    public function get_client_mch_action(){
        $input = $this->check();
        $id = $input['id'];
        $appInfo = AppInfoTableService::getBase($id);
        $appClient = AppClientTableService::getAllByFilter([
            'vendor' => $appInfo['vendor'],
        ]);
        $appMch = AppMchTableService::getAllByFilter([
            'aid' => $id,
        ]);
        $res = [
            'appId' => array_column($appClient,'aid'),
            'mids' => array_column($appMch,'mid'),
        ];
        return self::ok($res);
    }

    /**
     * 获取app绑定的支付商户和三方授权关系
     */
    public function set_client_mch_action(){
        $input = $this->check();
        $id = $input['id'];
        $appInfo = AppInfoTableService::getBase($id);
        AppClientTableService::setRelationByAppInfo($appInfo,$input['appIds']);
        AppMchTableService::setRelationByAppInfo($appInfo,$input['mids']);
        return self::ok();
    }
}